package com.itheima.bakery.demos.web.service.impl;


import com.itheima.bakery.demos.web.model.Inventory;
import com.itheima.bakery.demos.web.Repositories.InventoryRepository;
import com.itheima.bakery.demos.web.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class InventoryServiceImpl implements InventoryService {

    private final InventoryRepository inventoryRepository;

    @Autowired
    public InventoryServiceImpl(InventoryRepository inventoryRepository) {
        this.inventoryRepository = inventoryRepository;
    }

    @Override
    public List<Inventory> findAllInventory() {
        return inventoryRepository.findAll();
    }

    @Override
    public Optional<Inventory> findInventoryByProductID(Integer productID) {
        return inventoryRepository.findByProductID(productID);
    }

    @Override
    public Inventory saveInventory(Inventory inventory) {
        return inventoryRepository.save(inventory);
    }

    @Override
    public boolean deleteInventory(Integer productID) {
        Optional<Inventory> inventoryOptional = inventoryRepository.findById(productID);
        if (inventoryOptional.isPresent()) {
            inventoryRepository.deleteById(productID);
            return true;
        } else {
            return false;
        }
    }
}
